
** Loading raw data on social assistance (SA) from tax authority (SKAT)
forval t = 2012/2015{
	use "$data\minwage_SA_full_a700_`t'_bin.dta", clear

** Using spinal data from data01 to seleect individuals from cohorts 1992-1999
merge m:1 pnr using "$data\minwage_data01_pnr_key_cohort1992_1999.dta", keep(matched) keepus(pnr) nogen
destring korkod, replace

*** A few steps to correct or drop mis-filed payroll records
** Droping records and corrections. According to SKAT all corrections take the form of
** minus the original (wrong) recod and then a potential new and corrected record
** can be filed.
ge correction = ibertvirkidkod if korkod == 0
replace correction = referidkod if korkod == 1 | korkod == 2
bysort pnr senr indkaar angivpernr correction: egen delete = max(korkod)
drop if delete != 0
drop delete korkod ibertvirkidkod referidkod jobkod

** constructing a month variable
if `t' == 2013{
	ge month = mofd(angivpernr) - 575
}
if `t' == 2012 | `t' >= 2014{
	ge month = mofd(date(angivpernr, "YM")) - 575
}

** Collapsing data by indvidual, firm id and month to merge with firm information
collapse (sum) felt_200 = felt_200 (sum) pensblb (sum) timeantF12, by(pnr senr month) 
destring senr, force replace
** Merging firm information
merge m:1 senr using "$data\ES2015.dta", keep(master matched) keepus(es7604_dr_form_kod es7606_hov_br_nr) nogen		

** Collapsing data by indvidual and month: monthly payments
gsort pnr month -felt_200 // sort by indv month and largest payments
by pnr month: gen n = _n // only above 1 for indv with more than 1 monthly wage payment (has to be different firms)

foreach var in senr es7604_dr_form_kod es7606_hov_br_nr{
	replace `var' = 0 if n > 1 // keep firm information from the highest paycheck (by month)
}

collapse (max) n (sum) felt_200 (sum) timeantF12 (sum) pensblb (sum) senr (sum) es7604_dr_form_kod (sum) es7606_hov_br_nr ///
		, by(pnr month)

save "$data\minwage_data08_SA_`t'_04022019.dta", replace
}
